package cn.yunhe.controller;

import cn.yunhe.entity.Topic;
import cn.yunhe.entity.User;
import cn.yunhe.service.ItopicService;
import cn.yunhe.service.implService.TopicService;
import cn.yunhe.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "AddTopicServlet",urlPatterns = "/AddTopicServlet")
public class AddTopicServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String  title=request.getParameter("title");
        String  context=request.getParameter("context");
        HttpSession session=request.getSession();
        User user= (User) session.getAttribute("user");

        String  ptime=DateUtil.date2StringDateTime(new Date());

        Topic topic=new Topic();
        topic.setTitle(title);
        topic.setContext(context);
        topic.setPtime(ptime);
        topic.setUser(user);

        ItopicService topicService=new TopicService();
        boolean flag=topicService.addTopic(topic);
        response.sendRedirect("user_guestbook.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
